package com.me.mygdxgame;

import java.util.Iterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class GameScreen implements Screen  {

	final Egg ga;
	FileHandle file;
	Texture eggImage;
	Texture bucketImage;
	Texture chickenImage;
	Texture duckImage;
	Texture geeseImage;
	Texture shitImage;
	Texture treeImage;
	OrthographicCamera camera;
	Rectangle bucket;
	Rectangle tree;
	Array<Rectangle> eggdrops;
	Array<Rectangle> shitdrops;
	Array<Rectangle> chickens;
	Array<Long> chickenEggTime;
	Array<Integer> chickenWay;
	Array<Rectangle> ducks;
	Array<Long> duckEggTime;
	Array<Integer> duckWay;
	Array<Rectangle> geeses;
	Array<Long> geeseEggTime;
	Array<Integer> geeseWay;
	long lastEggTime;
	long lastPressTime;
	int life = 3;
	String highscore;
	public	GameScreen(final Egg game)
	{
		ga= game;
		ga.score = 0;
		file = Gdx.files.local("data/score.txt");
		if(file.exists()){
			  highscore = file.readString();
		}
		System.out.println(highscore);
		Texture.setEnforcePotImages(false);
		eggImage = new Texture(Gdx.files.internal("egg.png"));
	    bucketImage = new Texture(Gdx.files.internal("Ro.png"));
	    chickenImage = new Texture(Gdx.files.internal("ga.png"));
	    duckImage = new Texture(Gdx.files.internal("vit.png"));
	    geeseImage = new Texture(Gdx.files.internal("ngan.png"));
	    shitImage = new Texture(Gdx.files.internal("shit.png"));
	    treeImage = new Texture(Gdx.files.internal("tree.png"));
	    
	    camera = new OrthographicCamera();
	    camera.setToOrtho(false, 800, 480);
	      // create bucket 
	    bucket = new Rectangle();
	    bucket.x = 800 / 2 - 64 / 2;
	    bucket.y = 20; 
	    bucket.width = 80;
	    bucket.height = 65;
	    
	    //create rec tree 
	    tree = new Rectangle();
	    tree.x= 0;
	    tree.y = 363;
	    tree.width = 800;
	    tree.height = 17;
	    

	    eggdrops = new Array<Rectangle>();
	    chickens = new Array<Rectangle>();
	    ducks = new Array<Rectangle>();
	    geeses = new Array<Rectangle>();
	    shitdrops = new Array<Rectangle>();
	    chickenEggTime = new Array<Long>();
	    duckEggTime = new Array<Long>();
	    geeseEggTime = new Array<Long>();
	    chickenWay = new Array<Integer>();
	    duckWay = new Array<Integer>();
	    geeseWay = new Array<Integer>();
	}
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		//TODO set color
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
	    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	   
	    camera.update();
	    //TODO ga.batch : draw all 
	    ga.batch.setProjectionMatrix(camera.combined);
	    // begin draw 
	    ga.batch.begin();
	    
	    ga.batch.draw(bucketImage, bucket.x, bucket.y);
	    ga.batch.draw(treeImage, tree.x, tree.y);
	    ga.font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
	    ga.font.draw(ga.batch, "" + ga.score, 120, 470);
	    
	    ga.font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
	    ga.font.draw(ga.batch, ""+ life, 25, 470);
	    
	    //TODO create chickens, ducks, geeses, eggs from their array
	    for(Rectangle chicken: chickens) {
	    	ga.batch.draw(chickenImage, chicken.x, chicken.y);
	    }
	    for(Rectangle duck: ducks) {
	    	ga.batch.draw(duckImage, duck.x, duck.y);
	    }
	    for(Rectangle geese: geeses) {
	    	ga.batch.draw(geeseImage, geese.x, geese.y);
	    }
	    for (Rectangle egg : eggdrops) {
				ga.batch.draw(eggImage, egg.x, egg.y);
		}
	    for (Rectangle shit : shitdrops) {
			ga.batch.draw(shitImage, shit.x, shit.y);
	    }
	    ga.batch.end();
	      
	    //TODO input key in game
	    InputKey();
	    //TODO move and create egg & shit  
	    AllMove(chickens);
		AllMove(ducks);
	    AllMove(geeses);
	    
	    
	    //TODO iterator create move when render
	    Iterator<Rectangle> iter = eggdrops.iterator();
	    while(iter.hasNext()) {
	    	Rectangle egg = iter.next();
	    	if(ga.score / 400 > 0)
	    		egg.y -= 200 *(1 + (ga.score / 400 * 0.2)) * Gdx.graphics.getDeltaTime();
	    	else
	    		egg.y -= 200 *  Gdx.graphics.getDeltaTime();
		    if(egg.y + 64 < 0)
		    {
		    	iter.remove();
		    	life -= 1;
		    	if(life == 0)
		    	{
		    		if(highscore != null && highscore != "")
		    			if(Float.valueOf(highscore) < ga.score)
		    				highscore =String.valueOf(ga.score);
	            	file.writeString(highscore, false);
	            	dispose();
		    		ga.setScreen(new GameOverScreen(ga));
		    	}
		    }
		    if(egg.overlaps(bucket)) {
		    	ga.score += 20;
		    	iter.remove();
		    }
	    }
	    Iterator<Rectangle> iter2 = shitdrops.iterator();
	    while(iter2.hasNext()) {
	         Rectangle egg = iter2.next();
	         if(ga.score / 400 > 0)
		    		egg.y -= 200 *(1 + (ga.score / 400 * 0.2)) *  Gdx.graphics.getDeltaTime();
	         else
		    		egg.y -= 200 *  Gdx.graphics.getDeltaTime();
	         if(egg.y + 64 < 0) 
	         {
	        	 iter2.remove();
	         }
	         if(egg.overlaps(bucket)) {
	            iter2.remove();
	            life -= 1;
	            if(life == 0)
		    	{
	            	if(highscore != null && highscore != "")
	            		if(Float.valueOf(highscore) < ga.score)
	            			highscore =String.valueOf(ga.score);
	            	file.writeString(highscore, false);
	            	dispose();
	            	ga.setScreen(new GameOverScreen(ga));
		    	}
	         }
	    }
	}

	//TODO create bird, index and way in their array
	private void makerectanglebird(int type) {
		Rectangle bird = new Rectangle();
		bird.x = MathUtils.random(0, 800-64);
	    bird.y = 380;
	    bird.width = 64;
	    bird.height = 69;
	    if(type == 0)
		{
			chickens.add(bird);
			chickenEggTime.add(TimeUtils.millis());
			chickenWay.add(0);
		}
		else if(type == 1) {
			ducks.add(bird);
			duckEggTime.add(TimeUtils.millis());
			duckWay.add(0);
		}
		else {
			geeses.add(bird);
			geeseEggTime.add(TimeUtils.millis());
			geeseWay.add(0);
		}
	    lastPressTime = TimeUtils.nanoTime();
	}
	//TODO Make egg & shit
	private void makeEgg(Rectangle rec)
	{
		Rectangle egg = new Rectangle();
		egg.x = rec.x;
	    egg.y = rec.y;
	    egg.width = 45;
	    egg.height = 54;
	    if(MathUtils.random(0, 1) == 0)
	    	eggdrops.add(egg);
	    else
	    	shitdrops.add(egg);
	}
	//TODO move birds and 5s to create eggs
	public void AllMove(Array<Rectangle> arr)
	{
		for (int i = 0; i < arr.size ; i++) {
			Rectangle rec = arr.get(i);
			
			if (arr == chickens) {
				chickenWay.set(i, Dichuyen(rec,chickenWay.get(i)));
				if(TimeUtils.millis() - chickenEggTime.get(i) > 5000)
				{
					makeEgg(rec);
					chickenEggTime.set(i, TimeUtils.millis());
				}
			}
			else if(arr == ducks)
			{
				duckWay.set(i, Dichuyen(rec, duckWay.get(i)));
				if(TimeUtils.millis() - duckEggTime.get(i) > 5000)
				{
					makeEgg(rec);
					duckEggTime.set(i, TimeUtils.millis());
				}
			}
			else
			{
				geeseWay.set(i, Dichuyen(rec, geeseWay.get(i)));
				if(TimeUtils.millis() - geeseEggTime.get(i) > 5000)
				{
					makeEgg(rec);
					geeseEggTime.set(i, TimeUtils.millis());
				}
			}
		}
	}
	//TODO di chuyen trai hoac phai
	public int Dichuyen(Rectangle rec, int way)
	{
		if(rec.x +64 < 800 && way == 0)
		{
    		rec.x += 200*Gdx.graphics.getDeltaTime();
    		return 0;
		}
    	else
    	{
    		if(rec.x > 0)
    		{
    			rec.x -= 200*Gdx.graphics.getDeltaTime();
    			return 1;
    		}
    		else
    		{
    			return 0;
    		}
    	}
	}
	//TODO Input key
	public void InputKey()
	{
		if(Gdx.input.isTouched()) {
	    	Vector3 touchPos = new Vector3();
	    	touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	    	camera.unproject(touchPos);
	    	if(touchPos.x < 40)
	    		bucket.x = 0;
	    	else if(touchPos.x > 760)
	    		bucket.x = 720;
	    	else
	    		bucket.x = touchPos.x - 80 / 2;
	    }
		if(Gdx.input.isKeyPressed(Keys.P))
	    {
	    	pause();
	    	
	    }
	    if(Gdx.input.isKeyPressed(Keys.G))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(0);
			}
	    	
	    }
	    if(Gdx.input.isKeyPressed(Keys.V))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(1);
			}
	    }
	    if(Gdx.input.isKeyPressed(Keys.N))
	    {
	    	if (TimeUtils.nanoTime() - lastPressTime > 200000000 ) {
	    		makerectanglebird(2);
			}
	    }
	      
	    if(Gdx.input.isKeyPressed(Keys.LEFT) && bucket.x > 0 ) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
	    if(Gdx.input.isKeyPressed(Keys.RIGHT) && bucket.x + 80 < 800) bucket.x += 200 * Gdx.graphics.getDeltaTime();
	}
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		eggImage.dispose();
		bucketImage.dispose();
		chickenImage.dispose();
		duckImage.dispose();
		geeseImage.dispose();
		treeImage.dispose();
		eggdrops.clear();
		shitdrops.clear();
		chickens.clear();
		chickenEggTime.clear();
		chickenWay.clear();
		ducks.clear();
		duckEggTime.clear();
		duckWay.clear();
		geeses.clear();
		geeseEggTime.clear();
		geeseWay.clear();
	}

}
